<template>
	<view>
		<view class="content-padding">
			<view class="shouji">
				<view class="leftkuai"></view>
				<view class="rightText">
					<view class="">登录您的乐米账号</view>
					<!-- <view class="">请输入您的手机号码，登录或注册您的乐米账号</view> -->
				</view>
			</view>
		</view>
		<view class="" style="padding-left: 70rpx;">
			<view class="zhanghao">
				<image src="../../static/image/zhanghao.png" mode=""></image>
				<input type="number" v-model="mobile" placeholder="请输入手机号" />
			</view>
			<view class="zhanghao">
				<image src="../../static/image/yanzhengma.png" mode=""></image>
				<input type="text" v-model="password" password placeholder="请输入密码" />
			</view>

			<view class="zhanghao">
				<image v-if="tongyi" src="../../static/image/xuanzhong.png" mode="" @tap="tongyity(false)"></image>
				<image v-else src="../../static/image/weixuanzhong.png" mode="" @tap="tongyity(true)"></image>
				<view class="">我已阅读并接受<text @tap="toyonghuXieyi()">《服务协议》</text>与<text @tap="toXieyi()">《隐私政策》</text></view>
			</view>
		</view>
		<view class="content-padding">
			<view class="denglv" @tap="submit()">
				登录
			</view>
		</view>
		<view class="zhuce-wj">
			<view class="" @tap="toUrl('zhuce')">
				手机号注册
			</view>
			<view class="" @tap="toUrl('forgetpw')">
				忘记密码
			</view>
		</view>
		<selfPopupMessage ref="selfPopupMessage"></selfPopupMessage>
	</view>
</template>

<script>
	import permision from "@/js_sdk/wa-permission/permission.js"
	
	export default {
		data() {
			return {
				tongyi: true,
				mobile: "",
				password: ""
			}
		},
		onLoad: function() {
			
			// this.requestAndroidPermission('android.permission.RECORD_AUDIO');
			// this.requestAndroidPermission('android.permission.CAMERA');
			
			
			const value = uni.getStorageSync('token');

			if (value) {
				this.$util.toUrl("../index/index");

			}
		},
		methods: {
			async requestAndroidPermission(permisionID) {
				var result = await permision.requestAndroidPermission(permisionID)
				var strStatus
				if (result == 1) {
					strStatus = "已获得授权"
				} else if (result == 0) {
					strStatus = "未获得授权"
				} else {
					strStatus = "被永久拒绝权限"
				}
				// uni.showModal({
				// 	content: permisionID + strStatus,
				// 	showCancel: false
				// });
			},
			timLogin() {
				var that = this;
				let timtoken = uni.getStorageSync("timToken");
				let id = uni.getStorageSync("token");
				console.log("登陆token：" + timtoken);
				let promise = that.tim.login({
					userID: id,
					userSig: timtoken
				});
				promise.then(function(imResponse) {
					console.log(imResponse.data); // 登录成功
					if (imResponse.data.repeatLogin === true) {
						// 标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
						console.log(imResponse.data.errorInfo);
					}
				}).catch(function(imError) {
					console.log(JSON.stringify(imError));
					if (imError.code == 70001) {
						/**获取聊天token*/
						that.$util.nuiAjax(that.$jiekou.getTimToken, {
							userid: id
						}, function(res) {
							if (res.result == "true") {
								uni.setStorageSync("timToken", res.data);
								that.timLogin();
							}
						})
					} else {
						console.warn('login error:', imError); // 登录失败的相关信息
					}

				});
			},
			toXieyi: function() {
				this.$util.toUrl("./xieyi");
			},
			toyonghuXieyi:function(){
				this.$util.toUrl("./yonghuxieyi");
			},
			toUrl(url) {
				uni.navigateTo({
					url: "../" + url + "/" + url
				})
			},
			tongyity(isty) {
				this.tongyi = isty;
			},
			submit() {

				if (!this.tongyi) {
					this.$refs.selfPopupMessage.open("请先阅读并同意服务协议", "error");
					return;
				}
				if (this.$util.checkmobile(this.mobile)) {
					this.$refs.selfPopupMessage.open("请输入正确的手机号", "error");
					return;
				}
				if (this.password == "") {
					this.$refs.selfPopupMessage.open("请输入密码", "error");
					return;
				}
				var that = this;
				
				
				this.$util.nuiAjax(this.$jiekou.loginMobile, {
					mobile: this.mobile,
					password: this.password,
				}, function(res) {
					if (res.result == "true") {
						that.$refs.selfPopupMessage.open(res.message);
						uni.setStorageSync("token", res.data.id);
						uni.setStorageSync("userInfo", res.data);
						uni.$emit('userLogin');
						// uni.navigateBack();
						/**获取聊天token*/
						that.$util.nuiAjax(that.$jiekou.getTimToken, {
							userid: res.data.id
						}, function(res) {
							if (res.result == "true") {
								console.log(res.data);
								uni.setStorageSync("timToken", res.data);
								// that.timLogin();
								// let timtoken = uni.getStorageSync("timToken");
								// let id = uni.getStorageSync("token");
								// console.log("登陆token："+timtoken);
								// let promise = that.tim.login({userID: id, userSig: timtoken});
								// promise.then(function(imResponse) {
								//   console.log(imResponse.data); // 登录成功
								//   if (imResponse.data.repeatLogin === true) {
								//     // 标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
								//     console.log(imResponse.data.errorInfo);
								//   }
								// }).catch(function(imError) {
								//   console.warn('login error:', imError); // 登录失败的相关信息
								// });
								uni.switchTab({
									url: '/pages/index/index',
								});
							}
						})
						uni.getLocation({
							geocode:true,
							success:function(data){
								that.$util.nuiAjax(that.$jiekou.updateUser, {
									id: res.data.id,
									sheng:data.address.province,
									shi:data.address.city,
									qu:data.address.district
								}, function(result) {
									
								})
							}
						})
					} else {
						that.$refs.selfPopupMessage.open(res.message, "error");
					}
				})

			}
		}
	}
</script>

<style lang="scss">
	.content-padding {
		padding: 0 54upx 0 54upx;
	}

	.zhuce-wj {
		margin-top: 40rpx;
		display: flex;
		flex-flow: row;
		justify-content: space-between;
		color: $uni-text-color-placeholder;
		font-size: 28rpx;
		padding: 0 58rpx;
	}

	.denglv {
		height: 96rpx;
		background: $uni-color-primary;
		border-radius: 48rpx;
		margin-top: 80rpx;
		line-height: 96rpx;
		text-align: center;
		font-size: 32rpx;
		color: $uni-text-color-inverse;
		letter-spacing: 1rpx;
	}

	.zhanghao {
		display: flex;
		flex-flow: row;
		margin-top: 82rpx;
	}

	.zhanghao:nth-of-type(1) {
		margin-top: 100rpx;
	}

	.zhanghao text {
		color: $uni-color-primary;
	}

	.zhanghao view {
		height: 34rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: rgba(102, 102, 102, 1);
		line-height: 34rpx;
	}

	.zhanghao input {
		height: 42rpx;
		font-size: 30rpx;
		font-weight: 400;
		line-height: 42rpx;
	}

	.zhanghao:nth-of-type(3) {
		margin-top: 76rpx;
	}

	.zhanghao:nth-of-type(3) image {
		width: 28rpx;
		height: 28rpx;
		margin-right: 8rpx;
	}

	.zhanghao image {
		width: 28rpx;
		height: 32rpx;
		margin-right: 26rpx;
		margin-top: 4rpx;
	}

	.leftkuai {
		width: 6rpx;
		height: 34rpx;
		background: $uni-color-primary;
		margin-top: 9rpx;
	}

	.shouji {
		display: flex;
		flex-flow: row;
		margin-top: 130rpx;
	}

	.rightText :nth-of-type(1) {
		height: 50rpx;
		font-size: 36rpx;
		font-weight: 500;
		color: $uni-text-color;
		line-height: 50rpx;
	}

	.rightText {
		margin-left: 8rpx;
	}

	.rightText :nth-of-type(2) {
		margin-top: 16rpx;
		height: 34rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: $uni-text-color-grey;
		line-height: 34rpx;
	}
</style>
